package org.jjche.file.modules.file.api;

import lombok.RequiredArgsConstructor;
import org.jjche.file.modules.file.api.vo.FileVO;
import org.jjche.file.modules.file.service.FileExtService;
import org.jjche.file.modules.file.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 文件服务
 * </p>
 *
 * @author miaoyj
 * @since 2024-07-15
 */
@Service
@RequiredArgsConstructor
public class FileApiImpl implements FileApi{
    private final FileService fileService;
    private final FileExtService fileExtService;

    @Override
    public FileVO upload(MultipartFile file) {
        return fileExtService.upload(file);
    }

    @Override
    public FileVO uploadImage(MultipartFile file) {
        return fileExtService.uploadImage(file);
    }

    @Override
    public Boolean deleteUrls(Set<String> urls) {
        boolean delFile = fileExtService.delete(urls);
        boolean delFileRow = fileService.deleteByUrls(urls);
        return delFile && delFileRow;
    }

    @Override
    public void delete(List<String> ids){
        Set<String> filePaths = fileService.listUrlByIds(ids);
        fileExtService.delete(filePaths);
        fileService.deleteByIds(ids);
    }
}
